package com.durian.finance.service.impl;

import com.durian.common.core.utils.DateUtils;
import com.durian.finance.mapper.JrRentInstallmentMapper;
import com.durian.finance.service.IJrRentInstallmentService;
import com.durian.financial.api.domain.JrRentInstallment;
import com.durian.financial.api.domain.vo.JrRentInstallmentVO;
import com.durian.system.api.RemoteDictDataService;
import com.durian.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 房租分期申请Service业务层处理
 * 
 * @author durian
 * @date 2022-06-22
 */
@Service
public class JrRentInstallmentServiceImpl implements IJrRentInstallmentService 
{
    @Resource
    private JrRentInstallmentMapper jrRentInstallmentMapper;

    @Resource
    private RemoteDictDataService remoteDictDataService;

    /**
     * 查询房租分期申请
     * 
     * @param id 房租分期申请主键
     * @return 房租分期申请
     */
    @Override
    public JrRentInstallment selectJrRentInstallmentById(Long id)
    {
        return jrRentInstallmentMapper.selectJrRentInstallmentById(id);
    }

    /**
     * 查询房租分期申请列表
     * 
     * @param vo 房租分期申请表查询条件
     * @return 房租分期申请
     */
    @Override
    public List<JrRentInstallment> selectJrRentInstallmentList(JrRentInstallmentVO vo)
    {

        List<JrRentInstallment> list = jrRentInstallmentMapper.selectJrRentInstallmentList(vo);

        // 处理申请类型，借款期限，申请状态
        handleJrRentInstallment(list);

        return list;
    }

    /**
     * 新增房租分期申请
     * 
     * @param jrRentInstallment 房租分期申请
     * @return 结果
     */
    @Override
    public int insertJrRentInstallment(JrRentInstallment jrRentInstallment)
    {
        jrRentInstallment.setCreateTime(DateUtils.getNowDate());
        return jrRentInstallmentMapper.insertJrRentInstallment(jrRentInstallment);
    }

    /**
     * 修改房租分期申请
     * 
     * @param jrRentInstallment 房租分期申请
     * @return 结果
     */
    @Override
    public int updateJrRentInstallment(JrRentInstallment jrRentInstallment)
    {
        jrRentInstallment.setUpdateTime(DateUtils.getNowDate());
        return jrRentInstallmentMapper.updateJrRentInstallment(jrRentInstallment);
    }

    /**
     * 批量删除房租分期申请
     * 
     * @param ids 需要删除的房租分期申请主键
     * @return 结果
     */
    @Override
    public int deleteJrRentInstallmentByIds(Long[] ids)
    {
        return jrRentInstallmentMapper.deleteJrRentInstallmentByIds(ids);
    }

    /**
     * 删除房租分期申请信息
     * 
     * @param id 房租分期申请主键
     * @return 结果
     */
    @Override
    public int deleteJrRentInstallmentById(Long id)
    {
        return jrRentInstallmentMapper.deleteJrRentInstallmentById(id);
    }


    /**
     * 处理申请类型，申请状态
     */
    private void handleJrRentInstallment(List<JrRentInstallment> list){

        // 获取申请状态字典Map
        Map<String, String> applyStateMap = SysDictData.AjaxListToDictMap(remoteDictDataService.dictType("financial_apply_state"));

        // 获取申请类型字典Map
        Map<String, String> applyTypeMap = SysDictData.AjaxListToDictMap(remoteDictDataService.dictType("financial_apply_type"));

        list.forEach(s -> {

            // 设置金融产品类型
            s.setJrProductType(applyTypeMap.get(s.getJrProductType()));

            // 设置申请状态
            s.setApplyStateName(applyStateMap.get(s.getApplyState().toString()));
        });
    }

}
